package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.ycrlkj.android.modulescore.main.BR;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.XxOrderApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityXxOrderListBinding;
import com.ycrlkj.android.modulescore.main.entity.Query.XxStationQuery;
import com.ycrlkj.android.modulescore.main.entity.XxOrder;
import com.ycrlkj.android.modulespublic.common.base.DataBindingRecyclerViewAdapter;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_XX_ORDER_LIST)
public class XxOrderListActivity extends AppCompatActivity {

    @Inject
    XxOrderApiService xxOrderApiService;

    private ActivityXxOrderListBinding binding;

    private RecyclerView recyclerView;

    private DataBindingRecyclerViewAdapter<XxOrder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityXxOrderListBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initTitleBar();
        initAdapter();
        getAll();
    }

    public void initAdapter() {
        recyclerView = binding.recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DataBindingRecyclerViewAdapter<>(this, R.layout.item_xx_order, BR.xxOrder);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new DataBindingRecyclerViewAdapter.OnItemClickListener<XxOrder>() {
            @Override
            public void onItemClickListener(XxOrder item, int i) {
                String id = item.getId();
                ARouter.getInstance().build(RoutePath.PAGE_XX_ORDER_DETAIL).withString("id", id).navigation();
            }
        });
    }

    @SuppressLint("CheckResult")
    public void getAll() {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);
        XxStationQuery xxStationQuery = new XxStationQuery();
        xxOrderApiService.getAll(xxStationQuery)
                .compose(ResponseTransformer.obtain())
                .subscribe(xxOrder -> {
                            adapter.add(xxOrder);
                            XPopupUtil.hideLoading(loadingPopupView);
                        },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {
                                XPopupUtil.hideLoading(loadingPopupView);
                                ApiExceptionHandler.doWith(XxOrderListActivity.this, e);
                            }
                        });

    }

    public void initTitleBar() {

        MTitleBar titleBar = findViewById(R.id.m_title_bar);

        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                XxOrderListActivity.this.finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
            }
        });

    }


}